package com.spzx.channel.service.Impl;

import com.spzx.channel.service.IindexService;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.product.api.RemoteCategoryService;
import com.spzx.product.api.RemoteProductService;
import com.spzx.product.api.domain.ProductSku;
import com.spzx.product.api.domain.vo.CategoryVo;
import jakarta.servlet.ServletException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * projectName: spzx-parent
 *
 * @author: 王宝贵
 * time: 2024/10/8 17:15 周二
 * description:
 */
@Service
public class indexServiceImpl implements IindexService {

    @Autowired
    RemoteCategoryService remoteCategoryService;

    @Autowired
    RemoteProductService remoteProductService;

    /**
     * 用户端首页查询
     * @return 返回的是一个map集合
     */
    @Override
    public Map<String, Object> getIndex() throws ServletException {
        //获取所有商品的一级分类结果集
        R<List<CategoryVo>> RCategoryList = remoteCategoryService.getOneCategory(SecurityConstants.INNER);
        //如果出现异常
        if(RCategoryList.getCode()==R.FAIL){
            throw  new ServletException(RCategoryList.getMsg());
        }
        //获取畅销商品信息
        R<List<ProductSku>> RtopSaleList = remoteProductService.getTopSale(SecurityConstants.INNER);
        if(RCategoryList.getCode() == R.FAIL){
            //如果出现异常
            throw new ServletException(RtopSaleList.getMsg());
        }

        Map<String,Object> map = new HashMap<>();
        map.put("productSkuList",RtopSaleList.getData());
        map.put("categoryList",RCategoryList.getData());
        return map;
    }
}